/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TesteProteinas.java
 *
 * Created on Jul 28, 2012, 12:23:51 PM
 */
package epibot.testeProteinas;

import epibot.BancoDeDados.ArmazenaDados.Fasta;
import epibot.BancoDeDados.ArmazenaDados.ProcessaFasta;
import epibot.BancoDeDados.Tabelas;
import epibot.GUI.FramePrincipal;
import epibot.WebRobots.RegrasSitesPreditores.RegraDosSites;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;

/**
 *
 * @author Rafael Tosta
 */
public class JPanelTesteProteinas extends javax.swing.JPanel {

    private ArrayList<Fasta> fastas;
    private ArrayList<String> motores;
    private ImportProteinasTeste ipt;
    private String alelo, tamanho, nomeDaCalibragem;
    private FramePrincipal fp;

    /**
     * Creates new form TesteProteinas
     */
    public JPanelTesteProteinas(FramePrincipal fp) {
        initComponents();
        jPanelLoadFiles.setVisible(false);
        jPanelLoadTeste.setVisible(false);

        this.fp = fp;

        //inicia os calibradores
        mostraNomesCalibradores();
        buscaNotasSites();
    }

    public void configuracaoInicial() {
        //reseta as configurações
        this.fastas = null;
        this.jTextSequencias.setText("");
        this.jTextSequencias.setEditable(true);
        jPanelLoadFiles.setVisible(false);
        this.jTextInformations.setText("");
        this.jPanelLoadTeste.setVisible(false);
        mostraNomesCalibradores();
        buscaNotasSites();
        this.setPath("");

    }

    /**
     * Inicia a lista de alelos
     *
     * @return
     */
    private String[] iniciaListaDeAlelos() {
        return RegraDosSites.getPaserAleloTodosOsSites();
    }

    /**
     * Inicia a lista de tamanhos
     *
     * @return
     */
    private String[] iniciaListaDeTamanhos() {
        return RegraDosSites.getPaserTamanhoTodosOsSites();
    }

    private void mostraNomesCalibradores() {
        ArrayList<String> allNomesCalibragem = Tabelas.getTabelaNotaDoSite().getAllNomesCalibragem();
        String[] vetor = new String[allNomesCalibragem.size()];
        for (int i = 0; i < allNomesCalibragem.size(); i++) {
            vetor[i] = allNomesCalibragem.get(i);
        }

        jComboBoxCalibradores.setModel(new javax.swing.DefaultComboBoxModel(vetor));
    }

    private void buscaNotasSites() {
        nomeDaCalibragem = (String) this.jComboBoxCalibradores.getSelectedItem();
        motores = new ArrayList<String>();

        if (nomeDaCalibragem == null) {
            this.jTextInformations.setText("");
            this.jTextSitesNotes.setText("");
            this.jButtonDelCalib.setEnabled(false);
            return;
        }

        this.jButtonDelCalib.setEnabled(true);
        String nota, id;
        //buscar o alelo e tamanho
        DecimalFormat df = new DecimalFormat("0.##");
        ArrayList<String> y = Tabelas.getTabelaNotaDoSite().getNotaIdSite(nomeDaCalibragem);
        String x = Tabelas.getTabelaNotaDoSite().getAleloTamanho(nomeDaCalibragem);
        if (!x.equals("")) {
            String[] s1 = x.split(",");
            alelo = s1[0];
            tamanho = s1[1];
            this.jTextInformations.setText(" -> Allele: " + s1[0] + "\n -> Length: " + s1[1] + " mer");
            //busca as notas
            this.jTextSitesNotes.setText("");
            for (int i = 0; i < y.size(); i++) {
                String[] split = y.get(i).split(",");
                nota = split[0];
                id = split[1];

                String nomeSite = Tabelas.getTabelaSites().getNome(id);
                this.jTextSitesNotes.append(nomeSite + " = " + nota + "\n");
                motores.add(nomeSite);
            }
        }

    }

    public JProgressBar getjProgressBarLoadFiles() {
        return jProgressBarLoadFiles;
    }

    public JPanel getjPanelLoadFiles() {
        return jPanelLoadFiles;
    }

    public void setFastas(ArrayList<Fasta> fastas) {
        this.fastas = fastas;
    }

    public JPanel getjPanelLoadTeste() {
        return jPanelLoadTeste;
    }

    public JProgressBar getjProgressBarLoadTeste() {
        return jProgressBarLoadTeste;
    }

    public JTextArea getjTextSequencias() {
        return jTextSequencias;
    }

    public JTextArea getjTextInformations() {
        return jTextInformations;
    }

    
    //inicia as buscas nos sites
    public void buscaRobots() {

        this.fp.buscaTeste(fastas, motores, tamanho, alelo, nomeDaCalibragem);

    }

    public void setPath(String text) {
        this.jLabelPath.setText(text);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextSequencias = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jButtonSelectFile = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextSitesNotes = new javax.swing.JTextArea();
        jComboBoxCalibradores = new javax.swing.JComboBox();
        jButtonReset = new javax.swing.JButton();
        jButtonSubmit = new javax.swing.JButton();
        jPanelLoadFiles = new javax.swing.JPanel();
        jProgressBarLoadFiles = new javax.swing.JProgressBar();
        jPanelLoadTeste = new javax.swing.JPanel();
        jProgressBarLoadTeste = new javax.swing.JProgressBar();
        jLabel1 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextInformations = new javax.swing.JTextArea();
        jButtonDelCalib = new javax.swing.JButton();
        jLabelPath = new javax.swing.JLabel();
        jButtonCancel = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Proteins Test"));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Enter protein sequence(s)", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jTextSequencias.setColumns(20);
        jTextSequencias.setRows(5);
        jTextSequencias.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextSequenciasKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTextSequencias);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 20, 816, 220));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Or select file containing sequence(s)", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP));
        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jButtonSelectFile.setText("Select File");
        jButtonSelectFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSelectFileActionPerformed(evt);
            }
        });
        jPanel2.add(jButtonSelectFile);

        add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 250, 270, 50));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Select Calibrators", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP));
        jPanel3.setLayout(new java.awt.BorderLayout());

        jTextSitesNotes.setColumns(20);
        jTextSitesNotes.setEditable(false);
        jTextSitesNotes.setRows(5);
        jTextSitesNotes.setBorder(javax.swing.BorderFactory.createTitledBorder("Site's notes"));
        jScrollPane2.setViewportView(jTextSitesNotes);

        jPanel3.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jComboBoxCalibradores.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBoxCalibradores.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxCalibradoresActionPerformed(evt);
            }
        });
        jPanel3.add(jComboBoxCalibradores, java.awt.BorderLayout.PAGE_START);

        add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 310, 277, 220));

        jButtonReset.setText("Reset");
        jButtonReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonResetActionPerformed(evt);
            }
        });
        add(jButtonReset, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 550, 110, 40));

        jButtonSubmit.setText("Submit");
        jButtonSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSubmitActionPerformed(evt);
            }
        });
        add(jButtonSubmit, new org.netbeans.lib.awtextra.AbsoluteConstraints(640, 550, 110, 40));

        jPanelLoadFiles.setBorder(javax.swing.BorderFactory.createTitledBorder("Loading File(s)..."));
        jPanelLoadFiles.setLayout(new java.awt.BorderLayout());

        jProgressBarLoadFiles.setStringPainted(true);
        jPanelLoadFiles.add(jProgressBarLoadFiles, java.awt.BorderLayout.CENTER);

        add(jPanelLoadFiles, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 300, 220, 50));

        jPanelLoadTeste.setBorder(javax.swing.BorderFactory.createTitledBorder("Querying sites..."));
        jPanelLoadTeste.setLayout(new java.awt.BorderLayout());

        jProgressBarLoadTeste.setAutoscrolls(true);
        jProgressBarLoadTeste.setBorderPainted(false);
        jPanelLoadTeste.add(jProgressBarLoadTeste, java.awt.BorderLayout.CENTER);

        add(jPanelLoadTeste, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 480, 230, 50));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/epibot/testeProteinas/logo.gif"))); // NOI18N
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 290, -1, -1));

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Informations"));
        jPanel6.setLayout(new java.awt.BorderLayout());

        jTextInformations.setBackground(new java.awt.Color(240, 240, 240));
        jTextInformations.setColumns(20);
        jTextInformations.setEditable(false);
        jTextInformations.setFont(new java.awt.Font("Monospaced", 1, 13)); // NOI18N
        jTextInformations.setRows(5);
        jScrollPane3.setViewportView(jTextInformations);

        jPanel6.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        add(jPanel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(590, 349, 280, 180));

        jButtonDelCalib.setText("Delete Calibrator");
        jButtonDelCalib.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDelCalibActionPerformed(evt);
            }
        });
        add(jButtonDelCalib, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 530, 260, 30));
        add(jLabelPath, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 270, 530, 20));

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });
        add(jButtonCancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(760, 550, 110, 40));
    }// </editor-fold>//GEN-END:initComponents

private void jButtonSelectFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSelectFileActionPerformed
    buscaNotasSites();
    fastas = null;
    this.jTextSequencias.setText("");
    this.jTextSequencias.setEditable(true);
    //caso o usuario tente mandar dois carregamentos
//    if (ipt != null) {
//        ipt.stop();
//    }

    ipt = new ImportProteinasTeste(this);
    ipt.run();
}//GEN-LAST:event_jButtonSelectFileActionPerformed

private void jButtonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonResetActionPerformed
    configuracaoInicial();
}//GEN-LAST:event_jButtonResetActionPerformed

private void jButtonSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSubmitActionPerformed
    if (fastas == null) {
        String seq = this.jTextSequencias.getText();
        fastas = new ProcessaFasta().goString(seq);
        if (fastas != null) {
            jTextInformations.append("\n\n -> " + fastas.size() + " proteins selected");
        }
    }

    if (this.fastas != null) { //para or aquivos     
        //da inicio ao teste
        /**
         * 1 - busca a(s) proteinas inseridas 2 - realiza a consolidaçao
         */
        buscaRobots();

    } else {
        JOptionPane.showMessageDialog(this, "No protein sequence !");
    }
}//GEN-LAST:event_jButtonSubmitActionPerformed

private void jComboBoxCalibradoresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxCalibradoresActionPerformed
    buscaNotasSites();
}//GEN-LAST:event_jComboBoxCalibradoresActionPerformed

private void jTextSequenciasKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextSequenciasKeyReleased
//    String seq = this.jTextSequencias.getText();
//    fastas = new ProcessaFasta().goString(seq);
//    
//    if (this.fastas != null) {
//        jTextInformations.setText(" -> " + fastas.size() + " proteins selected");
//    }
}//GEN-LAST:event_jTextSequenciasKeyReleased

private void jButtonDelCalibActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDelCalibActionPerformed
    String a = (String) this.jComboBoxCalibradores.getSelectedItem();
    int x = JOptionPane.showConfirmDialog(this, "Remove the calibrator " + a + "?", "EPIBOT",
            JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE);

    if (x == JOptionPane.OK_OPTION) {
        Tabelas.getTabelaNotaDoSite().delete(a);//exclui os calibradores com o mesmo nome
        mostraNomesCalibradores();
        buscaNotasSites();
    }
}//GEN-LAST:event_jButtonDelCalibActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        fp.atualizaFrame();
    }//GEN-LAST:event_jButtonCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonDelCalib;
    private javax.swing.JButton jButtonReset;
    private javax.swing.JButton jButtonSelectFile;
    private javax.swing.JButton jButtonSubmit;
    private javax.swing.JComboBox jComboBoxCalibradores;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelPath;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanelLoadFiles;
    private javax.swing.JPanel jPanelLoadTeste;
    private javax.swing.JProgressBar jProgressBarLoadFiles;
    private javax.swing.JProgressBar jProgressBarLoadTeste;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextInformations;
    private javax.swing.JTextArea jTextSequencias;
    private javax.swing.JTextArea jTextSitesNotes;
    // End of variables declaration//GEN-END:variables
}
